package bloggo

import (
	"bytes"
	"fmt"
	"runtime"
	"strconv"
	"sync"
)

// 存储协程变量
var goGlobalMap sync.Map

func tset(k, v interface{}) {
	goGlobalMap.Store(fmt.Sprintf("%d_%v", goID(), k), v)
}

func tget(k interface{}) interface{} {
	if v, ok := goGlobalMap.Load(fmt.Sprintf("%d_%v", goID(), k)); ok {
		return v
	}
	return nil
}

func tdel(k interface{}) {
	sprintf := fmt.Sprintf("%d_%v", goID(), k)
	goGlobalMap.Delete(sprintf)
}

func goID() uint64 {
	b := make([]byte, 64)
	b = b[:runtime.Stack(b, false)]
	b = bytes.TrimPrefix(b, []byte("goroutine "))
	b = b[:bytes.IndexByte(b, ' ')]
	n, _ := strconv.ParseUint(string(b), 10, 64)
	return n
}
